package net.fenghaitao.imports;

import net.fenghaitao.context.ImportContext;
import net.fenghaitao.exception.AutoExcelException;
import org.apache.poi.util.XMLHelper;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class ExcelReader extends DefaultHandler {
    private ImportContext importContext;

    public ExcelReader(ImportContext importContext) {
        this.importContext = importContext;
    }

    /**
     * Traversing all the workbook spreadsheet
     */
    public void process(String filename) {
        try {
            ExcelDataHandler dataImporter = new ExcelDataHandler(importContext);
            dataImporter.process(filename);
        }
        catch (Exception e) {
            throw new AutoExcelException(e);
        }
    }

    public XMLReader fetchSheetParser() {
        try {
            XMLReader parser = XMLHelper.newXMLReader();
            parser.setContentHandler(new SheetHandler(importContext));
            return parser;
        }
        catch (Exception e) {
            throw new AutoExcelException(e);
        }
    }
}
